<template>
  <div class="crimeSceneList">
    <div class="crimeSceneList_left">
      <!-- 业务组织架构 -->
      <div v-show="orgFlag == '2'">
        <div class="organTit">
          <h3>业务组织架构</h3>
          <Button type="primary" @click="changeOrg(2)">切换为嘉扬架构</Button>
        </div>
        <Tree :data="treeListBusiness" @on-select-change="treeChangeBusiness" class="tree_render"></Tree>
      </div>
      <!-- OA组织架构 -->
      <div v-show="orgFlag == '1'">
        <div class="organTit">
          <h3>嘉扬组织架构</h3>
          <Button type="primary" @click="changeOrg(1)">切换为业务架构</Button>
        </div>
        <Tree :data="treeList" @on-select-change="treeChange" class="tree_render"></Tree>
      </div>
    </div>
    <div class="crimeSceneList_right">
      <right-list ref="list" :treeList="treeList"></right-list>
    </div>
  </div>
</template>

<script>
  import rightList from './rightList.vue'
  import crimeOpt from './opt/listOpt'
  import organApi from '@/biz/backStage/organizationManagement'
  import secret from '@/common/secretCode.js'
  export default {
    components: {
      rightList
    },
    data() {
      return {
        treeList: [],
        orgFlag: '2', //组织架构切换(1: OA 2: 业务)
        treeListBusiness: [],
        BusinessOrgId: '',
        OAorgId: '',
        BusinessOrg: {}
      }
    },

    computed: {},
    watch: {},
    methods: {
      //切换
      changeOrg(type) {
        console.log(this.treeListBusiness, this.treeList)
        if (type == 1) {
          this.orgFlag = '2'
          this.treeListBusiness.length > 0 &&
            this.$refs.list.getOrgId(
              {
                caseLocationId: this.BusinessOrgId
              },
              this.orgFlag,
              1
            )
        } else {
          this.orgFlag = '1'
          this.treeList.length > 0 &&
            this.$refs.list.getOrgId(
              {
                caseLocationId: this.OAorgId
              },
              this.orgFlag,
              1
            )
        }
      },
      //获取部门列表
      getDepartmentList() {
        let parmas = {
          appId: this.$dt.userInfo().appId,
          userId: this.$dt.userInfo().userId,
          roleId: this.$dt.userInfo().role.roleId,
          delFlag: 2
        }
        crimeOpt.getDepartmentList(parmas).then((res) => {
          this.treeList = res
          if (res.length > 0) {
            this.OAorgId = res[0].orgId
          }
        })
      },
      treeChange(event) {
        console.log(event, '点击了树结构')
        if (!event.length) {
          this.treeList[0].selected = true
          this.OAorgId = this.treeList[0].orgId
        } else {
          this.OAorgId = event[0] ? event[0].orgId : ''
        }
        let params = {
          caseLocationId: this.OAorgId
        }
        this.$refs.list.getOrgId(params, this.orgFlag, 1)
      },
      //获取业务组织架构
      getBusinessList() {
        crimeOpt.getBusinessList().then((res) => {
          this.treeListBusiness = res
          if (res.length > 0) {
            this.BusinessOrgId = res[0].orgId
            this.$refs.list.getOrgId(
              {
                caseLocationId: res[0].orgId
              },
              this.orgFlag,
              1
            )

            this.BusinessOrg = res[0]
            this.$refs.list.getNoOfficeNum(this.BusinessOrg)
          }
        })
      },
      treeChangeBusiness(event) {
        console.log(event, '点击了树结构')
        if (!event.length) {
          this.treeListBusiness[0].selected = true
          this.BusinessOrgId = this.treeListBusiness[0].orgId
          this.BusinessOrg = this.treeListBusiness[0]
        } else {
          this.BusinessOrgId = event[0] ? event[0].orgId : ''
          this.BusinessOrg = event[0]
        }
        let params = {
          caseLocationId: this.BusinessOrgId
        }
        this.$refs.list.getOrgId(params, this.orgFlag, 1)
        this.$refs.list.getNoOfficeNum(this.BusinessOrg)
      }

      //获取业务组织架构

      // digui(treeListBusiness, orgChoose, father) {
      //   treeListBusiness.forEach((item, index) => {
      //     if (item.orgId == orgChoose[0].orgId) {
      //       console.log(item, index, '3333333333');
      //       if (index != 0) {
      //         this.$set(father, 'expand', true)
      //         this.$set(treeListBusiness[index], 'selected', true)
      //         this.$set(this.treeListBusiness[0], 'selected', false)
      //       }

      //     }
      //     if (item.children && item.children.length) {
      //       this.digui(item.children, orgChoose, item)
      //     }
      //   })
      // },
      // getBusinessList() {
      //   crimeOpt.getBusinessList().then(res => {
      //     console.log(res, '我是业务组织架构');

      //     if (res && res.length > 0) {
      //       this.treeListBusiness = res
      //       let orgChoose = JSON.parse(this.$tool.getStorage('orgChoose'))
      //       if (orgChoose && orgChoose.length) {
      //         this.digui(this.treeListBusiness, orgChoose, null)
      //         this.BusinessOrg = orgChoose[0]
      //       } else {
      //         this.$set(this.treeListBusiness[0], 'selected', true)
      //         this.BusinessOrg = this.treeListBusiness[0]

      //       }

      //       this.$refs.list.getOrgId({
      //         caseLocationId: this.BusinessOrg.orgId
      //       }, this.orgFlag, 1)
      //       this.$refs.list.getNoOfficeNum(this.BusinessOrg)
      //       // this.treeListBusiness.length > 0 && this.$refs.list.checkOrgFun('business',
      //       //   this.BusinessOrg)
      //       // this.checkNodeTitle = this.getCheckNodeTitle([this.BusinessOrg]).join(
      //       //   '-')
      //       this.getBtnCode(this.BusinessOrg)
      //     }

      //   })
      // },
      // treeChangeBusiness(event) {
      //   console.log(event, '点击了树结构')
      //   if (!event.length) {
      //     this.treeListBusiness[0].selected = true
      //     this.BusinessOrgId = this.treeListBusiness[0].orgId
      //   } else {
      //     this.BusinessOrgId = event[0] ? event[0].orgId : ''
      //   }
      //   let params = {
      //     caseLocationId: this.BusinessOrgId,
      //   }
      //   this.$refs.list.getOrgId(params, this.orgFlag, 1)
      //   // 获取无业务组织案场
      //   console.log('333333333');
      //   this.$refs.list.getNoOfficeNum(event[0])
      //   console.log('4444444444444');

      //   console.log(event, '选中的业务组织');
      //   this.$tool.setStorage({ orgChoose: JSON.stringify(event) })

      //   // 点击业务组织更改btn的code
      //   this.getBtnCode(event[0])

      // },
      // getBtnCode(event) {
      //   if (this.$dt.userInfo().roleType == 2) {
      //     let parmas1 = {}
      //     if (event.authId) {
      //       parmas1.authId = event.authId
      //     } else {
      //       parmas1.orgId = event.orgId
      //     }
      //     organApi.clickMenuCode(parmas1)
      //   }
      // },
    },
    mounted() {
      this.getDepartmentList()
      this.getBusinessList()
    }
  }
</script>
<style lang="scss" scoped>
  .crimeSceneList {
    padding: 20px;
    height: 100%;
    width: 100%;
    display: flex;

    &_left {
      flex: 0 0 300px;
      padding: 0 10px;
      height: 100%;
      border: 1px solid #ccc;
      margin-right: 20px;
      border-radius: 8px;
      max-height: 100%;
      overflow-y: auto;
      position: relative;

      .organTit {
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        padding: 15px 10px;
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
      }

      .tree_render {
        margin-top: 62px;
      }
    }

    &_right {
      flex: 1;
      height: 100%;
      border: 1px solid #ccc;
      border-radius: 8px;
      padding: 20px;
    }

    ::v-deep .ivu-tree-title {
      width: calc(100% - 15px);
    }
  }
</style>
